﻿class Field extends Control implements ChangeEventExtended {
	public static var input:Number=0;
	public static var number:Number=1;
	public static var password:Number=2;
	public static var list:Number=3;
	public static var combo:Number=4;
	public static var WIDTH_LABEL:Number=50;
	public static var HEIGHT_LABEL:Number=20;
	public static var DELTA_LABEL:Number=10;
	
	private var text:TextControl;
	private var control:Control;
	private var label:String;
	private var type:Number;
	private var base:MovieClip;
	private var view;
	private var viewLabel:TextView;
		
	function Field(container:MovieClip, name:String, label:String, type:Number){
		super(container);
		setName(name);
		viewLabel=new TextViewLabelClassic();
		this.label=label;
		this.type=type;
		base=createMovieClip();
	}
	
	public function getControl():Control{
	return control;
	}
	public function setValueControl(control:Control){
	this.control=control;
	}
	
	public function reset(){
	control.reDraw();
	}
	
	public function setView(view):Void{
		if(type==Field.list){
			this.view=ListView(view); // lo casteo
		} else {
			this.view=TextView(view); // lo casteo
		}
	}
	public function setViewLabel(view):Void{
		this.viewLabel=view;
	}

	public function setLabel(label:String){
	text=new TextControl(base, getName(), viewLabel);
	text.setLocation(new Point(0 - WIDTH_LABEL - DELTA_LABEL, 0));
	text.setSize(new Point(WIDTH_LABEL, HEIGHT_LABEL));
	text.draw();
	text.setValue(label);
	}
	
	public function setWidthLabel(width:Number){
	WIDTH_LABEL=width;
	}
	public function setHeightLabel(height:Number){
	HEIGHT_LABEL=height;
	}
	public function setDeltaLabel(delta:Number){
	DELTA_LABEL=delta;
	}

	public function getLabelControl():Text{
	return text;
	}

	private function createControl(type:Number){
		var _size:Point=new Point(getSize().getX() - WIDTH_LABEL - DELTA_LABEL, getSize().getY());
		var _loc:Point=new Point(0,0);

		switch(type){
		case input:
			control=new TextControlInput(base, undefined, view);
			TextControlInput(control);
			control.setLocation(_loc);
			control.setSize(_size);
			TextControlInput(control).setViewText(view);
			TextControlInput(control).draw();
			control.addChangeEvent(this);
		break;
		case number:
			control=new TextControlNumber(base, undefined, view);
			TextControlNumber(control);
			control.setLocation(_loc);
			control.setSize(_size);
			TextControlNumber(control).setViewText(view);
			TextControlNumber(control).draw();
			control.addChangeEvent(this);
		break;
		case password:
			control=new TextControlPassword(base, undefined, view);
			TextControlPassword(control);
			control.setLocation(_loc);
			control.setSize(_size);
			TextControlPassword(control).setViewText(view);
			TextControlPassword(control).draw();
			control.addChangeEvent(this);
		break;
		case list:
			control=new ListControl(base, undefined, view);
			control.setLocation(_loc);
			control.setSize(_size);
			ListControl(control).draw();
			control.addChangeEvent(this);
		break;
		case combo:
			/*
			control=new ComboControl(base);
			control.setLocation(new Point(10,0));
			control.setSize(new Point(150, 20));
			control.reDraw();
			control.addChangeEvent(this);
			*/
		break;
		}
	}
	
	public function draw(){
		createControl(this.type);
		setLabel(this.label);
	}

	public function reDraw(){
		control.reDraw();
	}

	public function changedExtended(control:Control){
		propagateChangeEvent();
	}
	public function changed(){
	}

} // fin